package bktech.soict.hust.bknote.activity.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import bktech.soict.hust.bknote.R;
import bktech.soict.hust.bknote.activity.MainActivity;
import bktech.soict.hust.bknote.activity.SendSmsActivity;

import junit.framework.Protectable;

import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class sms extends Activity {

	List<smsMembers> aSMS = new ArrayList<smsMembers>();
	ArrayAdapter<smsMembers> adapter = null;
	ListView list;
	String[] thread_id;
	int[] msg_count;
	String[] snippet;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms);
		list = (ListView) findViewById(R.id.smslist);
		adapter = new smsAdapter();
		list.setAdapter(adapter);
		registerForContextMenu(list);
		final SmsHelper helper = MainActivity.helper;
		Thread createRow = new Thread(new Runnable() {

			public void run() {
				Cursor Cur = helper.queryInbox();
				if (Cur.moveToFirst()) {

					for (int i = 1; i <= Cur.getCount(); i++) {
						final String name;
						final String body;
						final long time;
						final String time_sms;
						if (Cur.getString(Cur
								.getColumnIndexOrThrow("display_name")) == null) {
							name = Cur.getString(Cur.getColumnIndex("address"));
						} else {
							name = Cur.getString(Cur
									.getColumnIndexOrThrow("display_name"));
						}
						body = Cur.getString(Cur.getColumnIndex("body"));
						time = Cur.getLong(Cur.getColumnIndexOrThrow("date"));

						SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM");
						Calendar calendar = Calendar.getInstance();
						calendar.setTimeInMillis(time);
						time_sms = formatter.format(calendar.getTime());

						runOnUiThread(new Runnable() {

							public void run() {
								// TODO Auto-generated method stub
								smsMembers thongTin = new smsMembers();
								thongTin.setName(name);
								thongTin.setText(body);
								thongTin.setTime(time_sms);
								adapter.add(thongTin);
							}
						});
						Cur.moveToNext();
					}
					Cur.close();
				}
			}
		});
		createRow.start();
		// Hien thi noi dung cua 1 conversations
		
		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> list, View view,
					int position, long arg3) {
				Intent i = new Intent(sms.this, conversation.class);
				Cursor Cur2 = helper.queryInbox();
				Cur2.moveToFirst();
				for(int j = 0; j < position; j++){
					Cur2.moveToNext();
				}
				String address = Cur2.getString(Cur2.getColumnIndexOrThrow("address"));
				String display_name = Cur2.getString(Cur2.getColumnIndexOrThrow("display_name"));
				i.putExtra("address", address);
				i.putExtra("display_name", display_name);
				startActivity(i);
			}
		});

	}// onCreate

	class smsAdapter extends ArrayAdapter<smsMembers> {
		TextView name;
		TextView txt;
		ImageView image;

		public smsAdapter() {
			super(sms.this, android.R.layout.simple_list_item_1, aSMS);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			SmsHolder holder = null;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row_sms1, parent, false);
				holder = new SmsHolder(row);
				row.setTag(holder);
			} else {
				holder = (SmsHolder) row.getTag();
			}
			holder.populateFrom(aSMS.get(position));
			return row;
		}

	}

	static class SmsHolder {
		TextView name;
		TextView txt;
		ImageView image;
		TextView time;

		public SmsHolder(View row) {
			name = (TextView) row.findViewById(R.id.number);
			txt = (TextView) row.findViewById(R.id.textSMS);
			time = (TextView) row.findViewById(R.id.time);
			image = (ImageView) row.findViewById(R.id.contact_image);
		}

		public void populateFrom(smsMembers r) {
			name.setText(r.getName());
			txt.setText(r.getText());
			time.setText(r.getTime());
			image.setImageResource(R.drawable.contact);
		}

	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		list = (ListView) findViewById(R.id.smslist);
		adapter = new smsAdapter();
		list.setAdapter(adapter);
		list = (ListView) findViewById(R.id.smslist);
		adapter = new smsAdapter();
		list.setAdapter(adapter);
		SmsHelper helper = MainActivity.helper;
		Cursor c = helper.queryInbox();
		switch(item.getItemId()){
		case R.id.tn_nhom1:
			adapter.clear();
			prioritySms(c, adapter, 1);
			adapter.notifyDataSetChanged();
			break;
		case R.id.tn_nhom2:
			adapter.clear();
			prioritySms(c, adapter, 2);
			adapter.notifyDataSetChanged();
			break;
		case R.id.tn_nhom3:
			adapter.clear();
			prioritySms(c, adapter, 3);
			adapter.notifyDataSetChanged();
			break;
		case R.id.tn_nhom4:
			adapter.clear();
			prioritySms(c, adapter, 4);
			adapter.notifyDataSetChanged();
			break;
		case R.id.tn_nhom5:
			adapter.clear();
			prioritySms(c, adapter, 5);
			adapter.notifyDataSetChanged();
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
		
	}
	public void prioritySms(Cursor c,ArrayAdapter<smsMembers> adapter,int priority){
		if(c.moveToFirst()){
			for(int i =1 ; i<= c.getCount();i++){
				int p = c.getInt(c.getColumnIndexOrThrow("priority"));
				if(p==priority){
					smsMembers thongTin = new smsMembers();
					final String name;
					final String body;
					final long time;
					final String time_sms;
					
					if (c.getString(c
							.getColumnIndexOrThrow("display_name")) == null) {
						name = c.getString(c.getColumnIndex("address"));
					} else {
						name = c.getString(c
								.getColumnIndexOrThrow("display_name"));
					}
					body = c.getString(c.getColumnIndex("body"));
					time = c.getLong(c.getColumnIndexOrThrow("date"));

					SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM");
					Calendar calendar = Calendar.getInstance();
					calendar.setTimeInMillis(time);
					time_sms = formatter.format(calendar.getTime());
					
					thongTin.setName(name);
					thongTin.setText(body);
					thongTin.setTime(time_sms);
					adapter.add(thongTin);
					
				}
				c.moveToNext();
			}
		}
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.context2, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		 if (item.getItemId()==R.id.search_sms) {
		      String message="selected search sms";
		      Toast.makeText(this, message, Toast.LENGTH_LONG).show();
		      Intent i = new Intent(this, Search.class);
	         startActivity(i);
		      return(true);
		    }
		    else if (item.getItemId()==R.id.sendsms) {
		    	 String message="selected Send sms";
			     Toast.makeText(this, message, Toast.LENGTH_LONG).show();
			     Intent i = new Intent(this, SendSmsActivity.class);
			     startActivity(i);
		      return(true);
		    }
		    else if (item.getItemId()==R.id.deletesms) {
		    	 String message="selected delete sms";
			      Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
			      
			      return(true);
			    }
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.sms_option, menu);
		return super.onCreateOptionsMenu(menu);
	}

}
